import java.util.Scanner;
public class Main {
    public static BTree CreatBT(String pre,String in){
        BTree bt = new BTree<>();
        bt.root = _CreatBT(pre,0,in,0,pre.length());
        return bt;
    }
    public static BTNode<Character> _CreatBT(String pre,int i,String in,int j ,int n){
        BTNode<Character> t;
        if(n<=0)
            return null;
        char ch;
        int pos,k;
        ch = pre.charAt(i);
        t = new BTNode<Character>(ch);
        pos = j;
        while(pos<j+n) {
            if(in.charAt(pos)==ch)
                break;
            pos++;
        }
        k = pos-j;
        t.left = _CreatBT(pre,i+1,in,j,k);
        t.right = _CreatBT(pre,i+k+1,in,pos+1,n-k-1);
        return t;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.next();
        String s2 = sc.next();

        BTree<Character> bt = CreatBT(s1,s2);

        System.out.println(bt.BracketOut());

        bt.PreOrder();

        bt.InOrder();

        bt.PostOrder();

        bt.LevelOrder();
    }
}